package m.common.model.util;

import m.system.exception.MException;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class FromQuerySql implements IModelQuery {
    private final StringBuilder allSql;
    private final List<String> fieldNames;//查询的属性
    private final List<Object> paramList;
    private final List<IModelQuery> unionList;
    private QueryPage page;

    public FromQuerySql(){
        this("");
    }
    public FromQuerySql(String sql){
        this.allSql=new StringBuilder();
        this.allSql.append(sql).append(" ");
        this.fieldNames=new ArrayList<>();
        this.paramList=new ArrayList<>();
        this.unionList=new ArrayList<>();
    }

    /**
     * 追加sql语句和参数
     */
    public FromQuerySql append(String sql,Object... params){
        allSql.append(sql).append(" ");
        paramList.addAll(Arrays.asList(params));
        return this;
    }

    /**
     * 追加包含解析字符的sql，参考ModelViewUtil
     */
    public FromQuerySql append(String sql,Map<String,Object> params){
        QueryParameter qp=new ModelViewUtil(sql,params).getParameter();
        allSql.append(qp.getSql()).append(" ");
        paramList.addAll(qp.getValueList());
        return this;
    }

    @Override
    public FromQuerySql addField(String... fieldName) {
        for(String f : fieldName) {
            if(!StringUtil.isSpace(f)&&!this.fieldNames.contains(f)) {
                this.fieldNames.add(f);
            }
        }
        return this;
    }
    @Override
    public FromQuerySql setPage(QueryPage page) {
        this.page=page;
        return this;
    }
    @Deprecated
    @Override
    public IModelQuery addFieldExpression(String field, String expression) {
        throw new RuntimeException("该方法无效，无需设置。");
    }

    @Deprecated
    @Override
    public IModelQuery addCondition(QueryCondition... cond) {
        throw new RuntimeException("该方法无效，无需设置。");
    }

    @Deprecated
    @Override
    public IModelQuery addOrder(QueryOrder... order) {
        throw new RuntimeException("该方法无效，无需设置。");
    }
    @Override
    public IModelQuery unionAll(IModelQuery... querys) {
        this.unionList.addAll(Arrays.asList(querys));
        return this;
    }

    @Override
    public String getAlias4Field(String field) {
        if(this.fieldNames.contains(field)){
            return field;
        }
        return null;
    }

    @Override
    public QueryParameter getQueryParameter() throws MException {
        StringBuilder sql=new StringBuilder(allSql.toString());
        if(!unionList.isEmpty()) {
            for (IModelQuery query : unionList) {
                QueryParameter qp = query.getQueryParameter();
                sql.append(" UNION ALL ").append(qp.getSubSql());
                this.paramList.addAll(qp.getValueList());
            }
        }else{
            if (null != this.page) {
                sql.append(this.page.toPageString());
            }
        }

        return new QueryParameter(sql.toString(),paramList);
    }

}
